include("util.js");
include("xml.js");

var karotz_ip="localhost"

var buttonListener = function(event) {
    if (event == "DOUBLE") {
        karotz.tts.stop();
        exit();
    }
    return true;
}

var exitFunction = function(event) {
    if((event == "CANCELLED") || (event == "TERMINATED")) {
        exit();
    }
    return true;
}

var onKarotzConnect = function(data) {
    karotz.button.addListener(buttonListener);

    karotz.tts.start("Please wait while I try to get your weather forecast", "en", exitFunction);
    var data = http.get("http://www.google.com/ig/api?weather=haifa,israel&hl=en");
    var parser = new XmlParser();
    var doc = parser.parse(data);
    
    karotz.tts.start("Forecast for " + doc.rootElement.weather.forecast_information.city.data, "en", exitFunction);
    karotz.tts.start("It is currently " + doc.rootElement.weather.current_conditions.condition.data, "en", exitFunction);
    karotz.tts.start("The temperature is " + doc.rootElement.weather.current_conditions.temp_c.data + " degrees Celcius", "en", exitFunction);
}

karotz.connectAndStart(karotz_ip, 9123, onKarotzConnect, {});
